define(['app'], function( app ) {
    'use strict';
    app.controller('Login', function ($rootScope, $scope, $compile, appFactory, dataFactoryCRM, $location, $filter, 
                                        $translate, settings, account, omcall, $timeout) {
        $scope.badPass = '甴曱騳驫鱻贔雦雥';//特殊密码作为记住密码的显示密码
        $scope.username;
        $scope.password;
        $scope.rememberPassword;
        $scope.loggingIn = false;
        $scope.loginError;
        $scope.isServiceOnline;
        dataFactoryCRM.getDataByAPI('Login/Index').then(function(data) {
            if($scope.user && data.goToURL){
                $scope.go(data.goToURL);
            }else if($scope.user){  //用户已经登录，浏览器访问login跳转到home页面
                $scope.go('/home');
            }
        });
        // if( $scope.user )
        //     $scope.go('/home');
        //记住密码
        var storage = window.localStorage;
        $scope.rememberPassword = (storage["rememberPassword"]!="false") ? true:false;
        if (undefined == storage["rememberPassword"])
            $scope.rememberPassword = false;
        if ($scope.rememberPassword && storage["password"]) {
            $scope.username = storage["username"];
            $scope.password = $scope.badPass
        }
        else{
            $scope.password  = '';
        }
        dataFactoryCRM.checkForInternetConnection(function (online) {
            if (!online)
                $scope.loginError = "无法连接到网络，请检查您的上网设置。";//$filter('translate')('NO_INTERNET_ACCESS');
        });

        dataFactoryCRM.isServiceOnline(function (online) {
            $scope.isServiceOnline = online;

            if (!online)
                $scope.loginError = "未能成功连接数据服务接口。";//$filter('translate')('SERVICE_OFFLINE');
        });

        $scope.closeError = function(){
            angular.element('.ident_notice').remove();
        }

        $scope.$on('userLoggedIn',function(d, data ){
            if( $scope.user && $scope.user.ext ){
                sessionStorage.ext = JSON.stringify( $scope.user.ext );
                omcall.initExt(  $scope.user.ext );
            }
        });

        $rootScope.$on('userLoggedOut',function(d, data ){
            //
        });

        $scope.login = function () {
            if (!$scope.isServiceOnline)
                return;
            $scope.loggingIn = true;
            if ($scope.password == $scope.badPass && $scope.rememberPassword)
                $scope.password = storage["password"];
            account.login($scope.username, $scope.password, '').then(function ( d ) {
                //记住密码
                var storage = window.localStorage;
                storage["rememberPassword"] = $scope.rememberPassword;
                if ($scope.rememberPassword) {
                    storage["username"] = $scope.username;
                    storage["password"] = d.remember;
                } else {
                    storage["username"] = "";
                    storage["password"] = "";
                }
                $scope.loginError = '';
                $rootScope.$broadcast('userLoggedIn','login');
                //若session中记录登录前访问模块，登陆后直接跳转，否则跳转进入home
                if(d.goToURL)   $location.path(d.goToURL);
                else    $location.path('/home');
            }, function (reason) {
                $scope.loggingIn = false;
                $scope.loginError = reason.msg;
            });
        }

        $scope.$watch('rememberPassword', function(newV, oldV){
            $scope.password = newV&&(storage["rememberPassword"]!="false") ? $scope.badPass : $scope.password;
            if (!newV && $scope.password == $scope.badPass )
                $scope.password = '';
        });
        
        $scope.path = $location.path();
        if($scope.path == "/login"){
            $("#header").hide();
        }
    });

    
});
